<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ForbiddenHttpException;
use yii\grid\GridView;
use yii\helpers\Html;
use backend\models\ScoreExchangeSearch;
use backend\models\PrfScoreExchange;
use backend\models\PayScoreItem;
use yii\base\Object;
use yii\base\Response;
use yii\widgets\ActiveForm;
/**
 * 金币兑换控制器
 * @author suiyue
 *
 */
class ScoreExchangeController extends BaseController
{
	/**
	 * (non-PHPdoc)行为
	 * @see \yii\base\Component::behaviors()
	 */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * (non-PHPdoc)前置函数
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	$action_list = array(
    			'index',
    			'update',
    			'view',
    			'create',
    			'delete'
    	);
    	 $access=$this->checkAccess($action_list,$action);//判断操作的action是否在actionlist里面
    	 $this->beforeOp('score-exchange/update', $access);//身份识别 权限验证  action验证
    	 return true;
    }
	/**
	 * 金币兑换列表
	 */
    public function actionIndex()
    {
    	$searchModel = new ScoreExchangeSearch();
    	$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    	     	return $this->render('index', [
    			'searchModel' => $searchModel,
    			'dataProvider' => $dataProvider,
    	]);
    }
    
    /**
     * 创建
     */
    public function actionCreate(){
    	$model = new PrfScoreExchange();
    	if ($model->load(Yii::$app->request->post())&&$model->validate()) {
    		$model->start_time=strtotime(Yii::$app->request->post()['PrfScoreExchange']['start_time']);
    		$model->end_time=strtotime(Yii::$app->request->post()['PrfScoreExchange']['end_time']);
    		if($model->last_total==""){
    			$model->last_total=$model->total;
    		}
    		//剩余件数不能大于总件数
    		$model->save();
    		return $this->redirect(['view', 'id' => $model->id]);
    	}else{
    		return $this->render('create', [
    				'model' => $model,
    		]);
    	}
    }
    
    /**
     * 修改
     */
    public function actionUpdate($id){
    	$model = $this->findModel($id);
    	if ($model->load(Yii::$app->request->post())) {
    		if($model->validate()){
    			$model->start_time=strtotime(Yii::$app->request->post()['PrfScoreExchange']['start_time']);
    			$model->end_time=strtotime(Yii::$app->request->post()['PrfScoreExchange']['end_time']);
    			//剩余件数不能大于总件数
    			if($model->last_total==""){
    				$model->last_total=$model->total;
    			}
    			$model->save();
    			return $this->redirect(['view', 'id' => $model->id]);
    		}else{
    			if($model->start_time==0){
	    			$model->start_time= date('Y-m-d H:i:s',time());
	    		}else{
	    			$model->start_time= date('Y-m-d H:i:s',$model->start_time);
	    		}
	    		
	    		if($model->end_time==0){
	    			$model->end_time= date('Y-m-d H:i:s',time());
	    		}else{
	    			$model->end_time= date('Y-m-d H:i:s',$model->end_time);
	    		}
    			return $this->render('update', [
    					'model' => $model
    			]);
    		}
    		
    	} else{
    		if($model->start_time==0){
    			$model->start_time= date('Y-m-d H:i:s',time());
    		}else{
    			$model->start_time= date('Y-m-d H:i:s',$model->start_time);
    		}
    		
    		if($model->end_time==0){
    			$model->end_time= date('Y-m-d H:i:s',time());
    		}else{
    			$model->end_time= date('Y-m-d H:i:s ',$model->end_time);
    		}
    		return $this->render('update', [
    			'model' => $model
    		]);
    	}
    }
    
    /**
     * 查看单条数据
     */
    public function actionView($id){
    	$this->layout = 'main_small';
    	$model = PrfScoreExchange::findOne($id);
    	if($model->start_time==""){
    		$model->start_time="立即开始";
    	}else{
    		$model->start_time=date("Y-m-d H:i:s",$model->start_time);
    	}
    	if($model->end_time==""){
    		$model->end_time="一直有效";
    	}else{
    		$model->end_time=date("Y-m-d H:i:s",$model->end_time);
    	}
    	return $this->render('view', [
    			'model' => $model
    	]);
    }
    /**
     * 查找单个
     * @param   $id
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = PrfScoreExchange::findOne($id)) !== null) {
        	$model = PrfScoreExchange::find()->where('id='.$id)->one();
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
   
    public function actionDelete($id)
    {
    	//判断是否已经使用，如果已经使用就不可以删除
    	$sql="select id from pay_score_item where ex_id=:ex_id";
    	$param=array(":ex_id"=>$id);
    	$payScore=PayScoreItem::findBySql($sql,$param)->exists();
    	if($payScore){
    		throw new NotFoundHttpException('已存在兑换记录，不可删除！');
    	}else{
    		$this->findModel($id)->delete();
    		return $this->redirect(['index']);
    	}
    	
    }
}
